<form class="datagrid-edit-form" data-toggle="validate"
      data-data-type="jsonp">
    <div class="bjui-row col-2">
        <label class="row-label">用户名</label>
        <div class="row-input required">
            <input type="text" name="name" id="user-name" class="form-control" data-rule="required">
        </div>
        <label class="row-label">密码</label>
        <div class="row-input required">
            <input type="password" name="password" id="user-password" class="form-control" data-rule="required">
        </div>
        <label class="row-label">角色</label>
        <div class="row-input required" >
            <select data-toggle="selectpicker" id="user-sysRole" data-width="228"  data-rule="required">
                <option value=""></option>
            </select>
        </div>
        <label class="row-label">电话</label>
        <div class="row-input required">
            <input type="text" name="tel" id="user-tel" class="form-control">
        </div>
        <label class="row-label">备注</label>
        <div class="row-input required">
            <input type="text" name="memo" id="user-memo" class="form-control">
        </div>
        <input type="hidden" id="user-getid" value="${id}">
        <input type="hidden" id="user-id">
        <input type="hidden" id="user-roid">
    </div>
    <div class="bjui-pageFooter">
        <ul>
            <li>
                <button type="button" class="btn-close btn" data-icon="close">取消</button>
            </li>
            <li>
                <button onclick="return commit()" class="btn-default btn" id="addUser" data-icon="save">保存</button>
            </li>
        </ul>
    </div>
</form>
<script>
    $(function () {
        getdata();
        $("#addUser").click(function () {
            BJUI.navtab('refresh', 'sysuser');
        })
    });

    function getdata() {
        var id = $("#user-getid").val();
        if (id !== "") {
            $.ajax({
                type: "POST",
                url: "/sysmanage/user/getmodule.do",
                data: {'id': id},
                cache: true,
                dataType: 'JSON',
                success: function (data) {
                    bindModule(data);
                    getsysrole();
                }
            });
        } else {
            getsysrole();
        }
    }

    function getsysrole() {
        var roid = $("#user-roid").val();
        var divRole = $("#user-sysRole");
        var options = "";
        //前台
        $.ajax({
            type: "POST",
            url: "/sysmanage/roles/roleList.do",
            data: "",
            cache: true,
            async: false,
            dataType: 'JSON',
            success: function (data2) {
                for (var i = 0; i < data2.length; i++) {
                    if (roid == data2[i].id) {
                        options += "<option value=" + data2[i].id + " selected=''>" + data2[i].name + "</option>";
                    } else {
                        options += "<option value=" + data2[i].id + ">" + data2[i].name + "</option>";
                    }

                }
                divRole.append(options);
                divRole.selectpicker('refresh');
                divRole.selectpicker('render');
            }
        })
    }

    function commit() {
        var id = $("#user-id").val();
        var name = $("#user-name").val();
        var password = $("#user-password").val();
        var roleId = $("#user-sysRole").val();
        var tel = $("#user-tel").val();
        var memo = $("#user-memo").val();
        var module = new Object();
        module.id = id;
        module.name = name;
        module.password = password;
        module.roleId = roleId;
        module.tel = tel;
        module.memo = memo;
        $.ajax({
            type: "POST",
            url: "/sysmanage/user/edit.do",
            data: JSON.stringify(module),
            dataType: 'json',
            cache: true,
            contentType: "application/json;charset=UTF-8",
            success: function (data) {
                console.log(data);
                if (data.Message === "ok") {
                    BJUI.alertmsg('ok', data.content)
                }
                debugger;
            }
        })
    }

    function bindModule(data) {
        $("#user-id").val(data.id);
        $("#user-name").val(data.name);
        $("#user-password").val(data.password);
        $("#user-tel").val(data.tel);
        $("#user-memo").val(data.memo);
        $("#user-roid").val(data.sysroleByRoleid.id);
    }
</script>
